/**
 * Copyright (C) 2024-2030 Super BilGen & 超亿时代信息技术股份有限公司.All Rights Reserved.
 */
package cn.com.yang.json;

import java.io.FileWriter;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import cn.com.yang.utils.FileUtil;

/**
 * @Model cn.com.yang.json.FormatJsonFile
 * @Desc  TODO
 * @author yangyijin
 * @since 2024年12月4日-下午1:51:53
 */
public class FormatJsonFile {
	
	String jsonFilePath = "C:\\Users\\小米电脑\\Desktop\\apis.json";
	/**
	 * 0.8~0.9s
	 */
	@Test
	public void test1() {
		String content = FileUtil.readJsonFileToStr(jsonFilePath);
		Map map = JSON.parseObject(content);
		System.out.print(map);
	}
	
	/**
	 * 0.5~0.7s
	 */
	@Test
	public void test2() {
		String content = FileUtil.readJsonFileToStr(jsonFilePath);
		Gson gosn = new Gson();
		Map map = gosn.fromJson(content, Map.class);
		List<Map> apiList = (List<Map>) map.get("apis");
		StringBuilder builder = new StringBuilder();
		List<String> returnList = new ArrayList();
		for(Map api : apiList) {
			List<Map> urlList = (List<Map>) api.get("urls");
			for(Map url :urlList) {
				String confUrl = (String) url.get("url");
				builder.append("api:").append(confUrl).append("\r\n");
				returnList.add(confUrl.substring(1));
			}
		}
		Collections.sort(returnList,new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
//				Collator com = Collator.getInstance(java.util.Locale.CHINA);
				Collator com = Collator.getInstance(java.util.Locale.US);
				return com.compare(o1, o2);
			}
		});
//		System.out.print(builder.toString());
		System.out.println(returnList.toString());
		String outPath = "C:\\Users\\小米电脑\\Desktop\\apis1.txt";;
		try(FileWriter writer =new FileWriter(outPath);) {
			for(String line : returnList) {
				writer.write("api:"+line+"\r\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 0.789 s
	 */
	@Test
	public void test3() {
		String content = FileUtil.readJsonFileToStr(jsonFilePath);
		ObjectMapper obj = new ObjectMapper();
		try {
			Map map = obj.readValue(content, Map.class);
			System.out.print(map);
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	}
}
